<?php 
//echo "<pre>";print_r($this->models);exit;
$db = &JFactory::getDbo();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Windsor Reward</title>
</head>

<body style="font-size: 14px; font-family: 'Courier New';">
<?php 
	//save to server
	$server_ip = '10.1.6.6';
	$port = '55759';
	
	$server_post = array();
	
	$server_post['TransactionID'] = JRequest::getInt('transaction_id');
	$server_post['OutletID'] = JRequest::getVar('outlet');
	$target_url = $server_ip.':'.$port.'/MemberService.svc/GetTransaction';
	
	// Encode $POST into JSON Type
	$json = str_replace('\/','/',json_encode($server_post));
	$result = WrdHelper::JSONSEND($target_url,$json);
	$pos = strpos($result, "POSTRANSACTION");
	//echo $result;exit;
	
	//echo $_SERVER["PHP_SELF"];exit;
	//var_dump($pos);exit;
	
	if($pos){
		
		$array_key = array('\/','\"','</POSTRANSACTION>"','"<?xml version="1.0" encoding="windows-1255"?>');
	
	$array_replace = array('/','"','</POSTRANSACTION>','');
	
	$result = str_ireplace($array_key,$array_replace,$result);
	
	$path = JPATH_SITE.'/logs-wrd/transactions/detailtransaction/';
	
	$str = '<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="'.JURI::root().'utilities/PosTransaction.xsl"?>';
	
	$string = $str.$result;
	if( strlen($result) > 0){
	
		$rs = file_put_contents($path.$server_post['TransactionID'].'.'.$server_post['OutletID'].'.xml',$string);
		//var_dump($rs);exit;
		if($rs){
			
			$app =& JFactory::getApplication();
			$app->redirect(JURI::root().'logs-wrd/transactions/detailtransaction/'.$server_post['TransactionID'].'.'.$server_post['OutletID'].'.xml');
			
		}
			else {
			
				echo "Lưu File thất bại"; exit;	
			}
		}
		else {
		
			echo "Kết quả trả về rỗng";exit;
		}
	}
	
	$array_key = array('\/','\"','</POSTRANSACTION>"','"<?xml version="1.0" encoding="windows-1255"?>','<POSTransactionClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">','"<?xml version="1.0" encoding="utf-16"?>','</POSTransactionClass>"');
	
	$array_replace = array('/','"','</POSTRANSACTION>','','<POSTransactionClass>','<?xml version="1.0" encoding="ISO-8859-1"?>','</POSTransactionClass>');
	
	$result = str_ireplace($array_key,$array_replace,$result);
	
	//echo $result."<hr/>";exit;
	
	$path = JPATH_SITE.'/logs-wrd/transactions/detailtransaction/';
	//echo $path;exit;
	/*<?xml-stylesheet type="text/xsl" href="'.JURI::root().'utilities/PosTransaction.xsl"?>*/
	$str = '<?xml version="1.0" encoding="ISO-8859-1"?>';
	
	$string = $str.$result;
	//echo $result;exit;
	if( strlen($result) > 0){
		
		
		$rs = file_put_contents($path.$server_post['TransactionID'].'.'.$server_post['OutletID'].'.xml',$result);
		//var_dump($rs);exit;
		$arr = array();
		if($rs){
			
			$xml=simplexml_load_file($path.$server_post['TransactionID'].'.'.$server_post['OutletID'].'.xml');
		?>
<table style="color: #666;" width="450" border="0" align="center">
  <tr>
    <td align="center"><?php echo $xml->HEADER_SLIP1; ?></td>
  </tr>
  <tr>
    <td align="center"><?php echo $xml->HEADER_SLIP2; ?></td>
  </tr>
  <tr>
    <td align="center"><?php echo $xml->HEADER_SLIP3; ?></td>
  </tr>
  <tr>
    <td align="center"><?php echo $xml->HEADER_SLIP4; ?></td>
  </tr>
  <tr>
    <td align="center"><?php echo $xml->HEADER_SLIP5; ?></td>
  </tr>
  <tr><td height="12"></td></tr>
  <tr>
    <td align="center">SALE #<?php echo $xml->TypeTransactionId;?> \ ORIGINAL</td>
  </tr>
  <tr>
    <td align="center">TRN #<?php echo $xml->TransactionId;?></td>
  </tr>
  <tr>
    <td align="center">TABLE #<?php echo $xml->TableName; ?></td>
  </tr>
  <tr>
    <td align="center">OPEN: <?php echo date("d/m/Y H:i",strtotime($xml->StartStamp)); ?></td>
  </tr>
  <tr>
    <td align="center">COVER: <?php echo $xml->PartySize;?></td>
  </tr>
  <!--
  <tr>
    <td align="center">REF :</td>
  </tr>-->
</table>
<table style="color:#666" width="450" border="0" align="center">
  <tr>
    <td colspan="2">ITEM</td>
    <td align="right" width="109">AMOUNT</td>
  </tr>
  <tr> <td style="border-top: dashed 2px #666;" colspan="3"></td></tr>
<?php $totalItem = 0;$gross = 0;
foreach($xml->TranasctionDetailList->anyType as $k => $v): ?>
<?php if($v->POSDepartmentId == 8) $sc = $v->DefaultAmount; ?>
<?php if(isset($v->POSDepartmentId) && ( $v->POSDepartmentId != 8) && $v->ReasonCode == 0): ?>
<tr>
	<td width="50"><?php echo $v->Quantity; $totalItem += $v->Quantity;?></td>
	<td width="419"><?php echo $v->Description; ?></td>
	<td align="right"><?php  echo number_format((Integer)($v->DefaultAmount),0,".",",");$gross += $v->DefaultAmount;?></td>
</tr>
<?php endif;?>
<?php endforeach;?>
<tr> <td style="border-top: dashed 2px #666;" colspan="3"></td></tr>
<tr>
	<td width="50">&nbsp;</td>
	<td colspan="2" width="419">
		<table border="0" width="100%">
			<tr>
				<td width="79%">TOTAL ITEMS </td>
				<td align="right"><?php echo $totalItem; ?></td>
			</tr>
			<tr>
				<td>GROSS</td>
				<td align="right"><?php echo number_format((integer)$gross,0,".",","); ?></td>
			</tr>
			<tr>
				<td>DISCOUNT</td>
				<td align="right"><?php echo number_format((Integer)($xml->DiscountTotal),0,".",","); ?></td>
			</tr>
			<tr>
				<td><?php echo $xml->Customer->CustomerData->ProgramName;?></td>
				<td></td>
			</tr>
			<tr> <td style="border-top: dashed 2px #666;" colspan="2"></td></tr>
			<tr>
				<td>SUB TOTAL</td>
				<td align="right"><?php  echo number_format((Integer)(($xml->SaleItemTotal - $sc)),0,".",","); ?></td>
			</tr>
			<tr>
				<td>SERVICE CHARGE 5%</td>
				<td align="right"><?php echo number_format((Integer)($sc),0,".",","); ?></td>
			</tr>
			<tr> <td style="border-top: dashed 2px #666;" colspan="2"></td></tr>
			<tr>
				<td>SUB TOTAL</td>
				<td align="right"><?php echo number_format((Integer)($xml->SaleItemTotal),0,".",","); ?></td>
			</tr>
			<tr>
				<td>TAX 10%</td>
				<td align="right"><?php echo number_format((Integer)($xml->TaxTotal),0,".",","); ?></td>
			</tr>
			<tr> <td style="border-top: dashed 2px #666;" colspan="2"></td></tr>
			
			<tr>
				<td>TOTAL</td>
				<td align="right"><?php echo number_format((Integer)($xml->TenderTotal),0,".",",");?></td>
			</tr>
			<tr>
				<td colspan="2" height="8"></td>
			</tr>
			
			<tr>
				<td><br/>RECEIVED <?php 
				 $paytype =  $xml->TranasctionDetailList->anyType[count($xml->TranasctionDetailList->anyType) - 1]->Description;
				 $paytype = strtoupper($paytype);
				 echo $paytype != 'CASH' ? $paytype : 'CASH' ;  
				?></td>
				<td align="right"><br/><?php echo number_format((Integer)($xml->TenderExChangeTotal),0,".",",");?></td>
			</tr>
			<tr> <td style="border-top: dashed 2px #666;" colspan="2"></td></tr>
			<?php if($paytype == 'CASH') :?>
			
			<tr>
				<td><br/>CHANGE CASH</td>
				<td align="right"><br/><?php echo number_format((Integer)($xml->TenderExChangeTotal - $xml->TenderTotal),0,".",",");?></td>
			</tr>
			<tr> <td style="border-top: dashed 2px #666;" colspan="2"></td></tr>
			<?php endif; ?>
		</table>
	</td>
</tr>

<tr><td colspan="3" height="12"></td></tr>

<tr>
	<td colspan="3">
		<table width="100%" border="0">
			<tr>
				<td>CUSTOMER NAME</td>
				<td >: <?php echo $xml->Customer->CustomerData->CustomerName;?></td>
			</tr>
			<tr>
				<td>CARD</td>
				<td>: <?php echo $xml->Customer->CustomerData->MagneticCard;?></td>
			</tr>
			<tr>
				<td>POINTS</td>
				<td>: <?php echo number_format((integer)($xml->Customer->CustomerData->PointsBalance),0,".",","); ?></td>
			</tr>
			
		</table>
	 </td>	
</tr>

<tr>
	<td colspan="3">
		<table width="100%" border="0">
			<tr>
				<td><br/>SHIFT: <?php echo $xml->ShiftName;?></td>
			</tr>
			<tr>
				<td>TR:<?php echo $xml->StoreId; ?> WS:<?php echo $xml->WorkstationId; ?>
					TRN#<?php echo $xml->TransactionId; ?> Z#<?php echo $xml->ZNumber; ?>
				    <!--RC:--><?php //echo $xml->RevenueCenterId; ?>
				</td>
				
			</tr>
			<tr>
				<td>CLOSED: <?php echo date("d/m/Y H:i",strtotime($xml->EndStamp)); ?></td>
				
			</tr>
		
		</table>
	 </td>	
</tr>    
</table>
<br/><br/><br/>	
<?php
		exit;
		echo "<hr/>";

		echo "<pre>";print_r($xml);
		exit;
		$app =& JFactory::getApplication();
		$app->redirect(JURI::root().'logs-wrd/transactions/detailtransaction/'.$server_post['TransactionID'].'.'.$server_post['OutletID'].'.xml');
	}
	else {
		echo "Lưu File thất bại";	
	}
	}
	else {
		echo "Kết quả trả về rỗng";
	}
	exit;	
?>
</body>
</html>
<?php 
   
    exit();
?>